package com.example.javamaday.leetcode;

import java.util.Arrays;
import java.util.HashMap;

public class LeetCodeTest0001 {

    public static void main(String[] args) {

        int[] ints = new LeetCodeTest0001().twoSum(new int[]{2, 7, 11, 15}, 9);

        System.out.println(Arrays.deepToString(new int[][]{ints}));
    }

    public int[] twoSum(int[] nums, int target) {
        HashMap<Integer, Integer> resultMap = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            // HashMap 查找时间复杂度为：O(1)
            if (resultMap.containsKey(nums[i])) {
                return new int[]{resultMap.get(nums[i]), i};
            } else {
                resultMap.put(target - nums[i], i);
            }
        }
        return new int[]{0, 0};
    }


}
